package JDBCDemo1;

import JdbcConnnectionUtils.JdbcUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class JDBCTemplete {

    private  JdbcTemplate  jt=new JdbcTemplate(JdbcUtils.getDatesource());
    /**
     * 1,更新数据库记录
     */
    @Test
    public  void  test1(){
        String sql="update worker set  banlance= 5000 where id= ? ";
        int count = jt.update(sql, 1);
        System.out.println(count);
    }
    /**
     * 2,删除数据库记录
     */
     @Test
    public void test2(){
         String sql="delete from worker where id=?";
         int count = jt.update(sql, 2);
         System.out.println(count);
     }
    /**
     * 3,查询单条记录封装到map中
     */
     @Test
     public  void test3(){
        String  sql="select*from emp where id= ?";
         Map<String, Object> stringObjectMap1 = jt.queryForMap(sql, 1001);

         System.out.println(stringObjectMap1);
     }
    /**
     * 4,查询所有数据封装到list中
     */
     @Test
     public void  test4(){
          String sql="select*from emp";
         List<Map<String, Object>> maps = jt.queryForList(sql);
         for (Map<String, Object> map : maps) {
             System.out.println(map);
         }


     }
    /**
     * 6,查询数据所有记录封装到map中存入list中
     */
     @Test
    public void test5(){
    String sql="select*from emp";
         List<Emp> query = jt.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
         for (Emp emp : query) {
             System.out.println(emp);
         }
     }

    /**
     * 7,查询总记录数
     */
    @Test
     public void test6(){
      String sql="select count(id) from emp";
         Long total = jt.queryForObject(sql, Long.class);
         System.out.println(total);
     }

    /**
     * 查询一条记录封装成javaBean对象
     *
     */
    @Test
     public  void test7(){
         String sql="select*from emp where id=?";
         Emp emp = jt.queryForObject(sql, new BeanPropertyRowMapper<Emp>(Emp.class), 1001);
         System.out.println(emp);
     }

    /**
     *查询一条记录封装成整数类型
     *
     */
    @Test
     public  void test8(){
        String sql="select count(id) from emp";
         Long total = jt.queryForObject(sql, Long.class);
         System.out.println(total);
     }

}
